package com.beamfield.controller.manage.operator;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.AttendMachine;
import com.beamfield.model.LeaveAnnex;
import com.beamfield.service.manage.operator.AttendService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 考勤机配置
 */
@Controller
@RequestMapping("/cms/operator/attend")
public class AttendController extends BaseController {

    private final AttendService attendService;

    @Autowired
    public AttendController(AttendService attendService) {
        this.attendService = attendService;
    }

    @RequestMapping(value = "/machine",method = RequestMethod.GET)
    public ModelAndView machine(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/attend",method = RequestMethod.GET)
    public ModelAndView attend(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/readMachineData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readMachineData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        List<AttendMachine> attendMachineList = attendService.gerMachineData(offset,limit,beamId);
        long total = attendService.countMachineData(beamId);
        return new PageResult(total, CommonUtils.parseLong(offset,0l),attendMachineList);
    }

    /**
     * 新增考勤机配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult save(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String attendDeviceId = request.getParameter("attend_device_id");
        String work = request.getParameter("work");
        if(StringUtils.isEmpty(attendDeviceId) || StringUtils.isEmpty(work)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            return attendService.save(beamId, userId, attendDeviceId, work);
        } catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 删除考勤机配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delete(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String attendDeviceId = request.getParameter("attend_device_id");
        if(StringUtils.isEmpty(attendDeviceId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return attendService.delete(beamId, userId, attendDeviceId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 更新考勤机配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult update(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String attendDeviceId = request.getParameter("attend_device_id");
        String oldAttendDeviceId = request.getParameter("old_attend_device_id");
        String work = request.getParameter("work");
        if(StringUtils.isEmpty(attendDeviceId)||StringUtils.isEmpty(oldAttendDeviceId)||StringUtils.isEmpty(work)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return attendService.update(beamId, userId, attendDeviceId, oldAttendDeviceId, work);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 人为修改考勤记录
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifyAttend",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult modifyAttend(HttpServletRequest request,String name,String date,Integer status){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);

        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(date)||status==null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return attendService.modifyAttend(name,date,status,beamId,userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 导出个人考勤信息
     * @return
     */

   @RequestMapping(value = "/exportManagerAttent")
    @ResponseBody
    public ApiResult exportTrain(Integer offset, Integer limit,String name,String year,HttpServletRequest request,HttpServletResponse response
                            ,String presasg,String workdept,String group){
       Long beamId = super.getBeamId(request);
       ApiResult apiResult=attendService.exportManagerAttent(offset,limit,name,year,beamId,response,presasg,workdept,group);
       return apiResult;
    }


    /**
     * 导出个人考勤信息
     * @return
     */

    @RequestMapping(value = "/exportManagerAttentAllByECode")
    @ResponseBody
    public ApiResult exportManagerAttentAllByECode(String ecode,String exportTime,HttpServletResponse response,HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        ApiResult apiResult=attendService.exportManagerAttentAllByECode(ecode,beamId,exportTime,response);
        return apiResult;
    }

    /**
     * 导出个人考勤信息
     * @return
     */

    @RequestMapping(value = "/exportManagerAttentAll")
    @ResponseBody
    public ApiResult exportManagerAttentAll(String workdept,String exportTime,HttpServletResponse response,HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        ApiResult apiResult=attendService.exportManagerAttentAll(workdept,beamId,exportTime,response);
        return apiResult;
    }

    /**
     * 获取考勤数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/readAttendData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult readAttendData(HttpServletRequest request){
        String month = request.getParameter("month");
        String workdept = request.getParameter("workdept");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(month) || StringUtils.isEmpty(workdept)){
            return null;
        }
        return attendService.getOperatorAttend(offset, limit, month, workdept, beamId);
    }

    /**
     * 上传请假附件
     * @return
     */
    @RequestMapping(value = "/saveLeaveAnnexFile",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveLeaveAnnexFile(@RequestParam("date") String date,@RequestParam("workdept") String workdept,
                                        HttpServletRequest request, @RequestParam("annexFile") MultipartFile multipartFile){
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(workdept) || multipartFile == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try {
            long fileId = super.saveFile((CommonsMultipartFile) multipartFile);
            LeaveAnnex leaveAnnex = new LeaveAnnex();
            leaveAnnex.setAddtime(new Date());
            leaveAnnex.setBeam_id(beamId);
            leaveAnnex.setFile_id(fileId);
            leaveAnnex.setYearmonth(date);
            leaveAnnex.setWorkdept(workdept);
            leaveAnnex.setOperator(userId);
            result = attendService.saveLeaveAnnex(leaveAnnex);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }
    }

    /**
     * 获取请假附件
     * @param request
     * @return
     */
    @RequestMapping(value = "/readLeaveAnnexFile", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readLeaveAnnexFile(HttpServletRequest request){
        String date = request.getParameter("date");
        String workdept = request.getParameter("workdept");
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(workdept)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        LeaveAnnex leaveAnnexFile = attendService.getLeaveAnnexFile(beamId, date, workdept);
        return new ApiResult(leaveAnnexFile);
    }
}
